package ui;


/**
 * @param args
 * @throws IOException
 */

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {

    public static void httpGetImg(String savePath) throws IOException {
        HttpURLConnection urlConnection = (HttpURLConnection) new URL("https://picsum.photos/1980/1080").openConnection();
        System.out.println(urlConnection.getResponseCode());

        InputStream in = urlConnection.getInputStream();
        FileUtils.copyInputStreamToFile(in, new File(savePath));
    }

    public static void main(String[] args) throws Exception {
//        List<String> unsplashRandomImages = ImagesDownload.getUnsplashRandomImages((byte) 10,"wallpapers");
//        List<String> pexelsSearchImages = ImagesDownload.getQiHu360Image(20,6,10);
//        System.out.println(pexelsSearchImages);
//
        imgGet("https://api.ixiaowai.cn/api/api.php");
    }

    public static void imgGet(String url) throws Exception {
        try {
            System.out.println(";;;;;;;;;;;;;;;;;;;;;;;;;;;>");
            CloseableHttpClient hc = HttpClients.createDefault();

            HttpGet get = new HttpGet(url);
            var hr = hc.execute(get);
            HttpEntity entity = hr.getEntity();
            InputStream in = entity.getContent();
            byte[] bytes = new byte[1024 * 1024 * 2];
            File file = new File("F:\\迅雷云盘\\DesktopBackground\\tes3.png");
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            int i = 0;
            while ((i = in.read(bytes)) != -1) {
                fos.write(bytes, 0, i);
            }
            fos.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 发送HttpGet请求
     *
     * @param url
     * @return
     */
    public static String doGet(String url) {
        //1.获得一个httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();

        //2.生成一个get请求
        HttpGet get = new HttpGet(url);
        get.setHeader("Authorization","563492ad6f91700001000001780c05ba42a74f438fd49998385075ca");
        CloseableHttpResponse response = null;
        try {
            //3.执行get请求并返回结果
            response = httpclient.execute(get);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String result = null;
        try {
            //4.处理结果，这里将结果返回为字符串
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
